type Detail = {
    art_id: string
    title: string
    pubdate: string
    aut_id: string
    content: string
    aut_name: string
    aut_photo: string
    is_followed: boolean
    is_collected: boolean
    attitude: number
    comm_count: number
    read_count: number
    like_count: number
  }

export type Comment = {
    aut_id: string
    aut_name: string
    aut_photo: string
    com_id: string
    content: string
    is_followed: boolean
    is_liking: boolean
    like_count: number
    pubdate: string
    reply_count: number
}

type CommentType = {
    end_id: string
    last_id: string
    total_count: number
    results: Comment[]
}

type ArticleType = {
    detail: Detail
    comment: CommentType
}

export type ArticelAction = 
    |   {
            type: 'artcile/saveDetail'
            payload: Detail
        }
    |   {
            type: 'artcile/saveComment'
            payload: CommentType
        }
    |   {
            type: 'artcile/saveMoreComment'
            payload: CommentType
        }
    |   {
            type: 'artcile/saveNewComment'
            payload: Comment
        }
    |   {
            type: 'artcile/updateComment'
            payload: Comment
        }

const initValue: ArticleType = {
    // 文章详情信息
    detail: {},
    // 评论信息
    comment: {}
} as ArticleType

export default function article(state = initValue, action: ArticelAction) {
    if (action.type === 'artcile/saveDetail') {
        return {
            ...state,
            detail: action.payload
        }
    }
    if (action.type === 'artcile/saveComment') {
        return {
            ...state,
            comment: action.payload
        }
    }
    if (action.type === 'artcile/saveMoreComment') {
        return {
            ...state,
            comment: {
                ...action.payload,
                results: [...state.comment.results, ...action.payload.results]
            }
        }
    }
    if (action.type === 'artcile/saveNewComment') {
        return {
            ...state,
            comment: {
                ...state.comment,
                results: [action.payload, ...state.comment.results]
            }
        }
    }
    if (action.type === 'artcile/updateComment') {
        return {
            ...state,
            comment: {
                ...state.comment,
                results: state.comment.results.map((item) => {
                    if (item.com_id === action.payload.com_id) {
                        return {
                            ...action.payload
                        }
                    } else {
                        return item
                    }
                })
            }
        }
    }
    return state
}